package com.monas.server.parser.linux;

import java.sql.Types;
import java.util.LinkedList;

import com.monas.server.parser.MonasHeader;
import com.monas.server.parser.MonasParser;

/**
 * 
 * @author eko wahyudi
 * @since Juli 2012
 * 
 */
public class FreeM extends MonasParser {

	public FreeM() {
		super(new MonasHeader(Types.INTEGER, "mem_total"), new MonasHeader(
				Types.INTEGER, "mem_used"), new MonasHeader(Types.INTEGER,
				"mem_free"));
	}

	@Override
	public boolean isNextBegin(String line) {
		boolean next = line.trim().indexOf("total") >= 0
				&& line.trim().indexOf("used") >= 0;
		return next;
	}

	@Override
	public boolean isFinish(String line) {
		return line.trim().startsWith("swap");
	}

	@Override
	public String getTableName() {
		return "free_m";
	}

	@Override
	public LinkedList<Object[]> getRecords() {
		LinkedList<Object[]> records = new LinkedList<Object[]>();
		if (getLines().size() > 0) {
			for (String line : getLines()) {
				if (line.startsWith("Mem")) {
					String[] fields = line.split("\\s+");
					String[] objects = { fields[1], fields[2], fields[3] };
					records.add(objects);
					break;
				}
			}
		}
		return records;
	}

}
